package backend.model.admin;

import backend.model.base.DBModel;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;

import javax.persistence.*;
import java.util.Set;

@NamedQueries({ @NamedQuery(name = "findAllTeams", query = "from Team"),
                @NamedQuery(name = "findAllActiveTeams", query = "from Team where isActive = true"),
                @NamedQuery(name = "findAllActiveTeamsOfForeman", query = "from Team t where isActive = true and t.foreman.id = :foremanId"),
                @NamedQuery(name = "findAllTeamsOfForeman", query = "from Team t where t.foreman.id = :foremanId"),
                @NamedQuery(name = "findTeamByName", query = "from Team where name = :name"),
                @NamedQuery(name = "findDefaultTeam", query = "from Team where id = 1")
             })

@Entity
@Table(name = "team")
public class Team extends DBModel {
	@Id
	@GeneratedValue
	@Column(name = "id")
	private int id;

	@Column(name = "name")
	private String name;

	@ManyToOne()
	@JoinColumn(name = "foreman_id")
	private Foreman foreman;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "team")
    private Set<Employee> employees;

    @Column(name = "is_external")
    private boolean isExternal;

    @Column(name = "is_active")
    private boolean isActive;

    @Override
    public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Foreman getForeman() {
		return foreman;
	}

	public void setForeman(Foreman foreman) {
		this.foreman = foreman;
	}

    public Set<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(Set<Employee> employees) {
        this.employees = employees;
    }

    public boolean getIsExternal() {
        return isExternal;
    }

    public void setIsExternal(boolean isExternal) {
        this.isExternal = isExternal;
    }

    public boolean getIsActive() {
        return isActive;
    }

    public void setIsActive(boolean isActive) {
        this.isActive = isActive;
    }

	@Override
    public String getDisplayLabel() {
		return name;
	}

	@Override
    public int compareTo(Object o) {
		if (!(o instanceof Team)) {
			return -1;
		} else {
			Team otherTeam = (Team) o;
			return getDisplayLabel().compareTo(otherTeam.getDisplayLabel());
		}
	}
}
